<template>
  <div id="repeated_maintenance"></div>
</template>

<script>
import * as echarts from 'echarts';

export default {
  props: ['data'],
  data() {
    return {
      echartsData:[],
      myChart:''
    }
  },
  mounted() {
    this.initEcharts()
    this.init()
  },
  activated() {
    this.init()
    document.querySelector(".ele-admin-content-view").style.height = "100%"
    this.refresh = true
  },
  deactivated() {
    document.querySelector(".ele-admin-content-view").style.height = "auto"
  },
  methods: {
    initEcharts(data, labels) {
      const chartDom = document.getElementById('repeated_maintenance')
      this.myChart = echarts.init(chartDom)
      this.myChart.off('click')
      const self = this
      this.myChart.on('click', function (params) {
        console.log(params,222)
        // 控制台打印数据的名称
        self.$emit('getTab', self.echartsData[params.dataIndex])
      });
      let option

      option = {
        grid:{
          left:"5%",
          bottom: "9%",
          right:"5%"
        },
        xAxis: {
          type: 'category',
          data:labels
        },
        tooltip: {
          trigger: 'item',
          // formatter: function(params){
          //   console.log(params)
          // }
        },
        color: "#108af1",
        yAxis: {
          type: 'value',
          name: "单位（天）",
          nameTextStyle: {
            color: "#aaa",
            nameLocation: "start",
          },
        },
        series: [
          {
            data: data,
            type: 'bar'
          }
        ]
      };

      option && this.myChart.setOption(option);
    },
    init() {
      window.addEventListener("resize", this.resizeList)
      this.$once('hook:beforeDestroy', function () {
        window.removeEventListener('resize', this.resizeList)
      })
    },
    resizeList() {
      if (this.myChart) {
        this.myChart.resize()
      }
    },
  },
  watch: {
    data({data, labels}) {
      this.initEcharts(data, labels)
    }
  }
}
</script>

<style scoped>
#repeated_maintenance {
  width: 100%;
  height: 500px;
}
</style>
